﻿using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;

namespace MediaGarden.Playlists.Mechanical
{
    public class Migrations : DataMigrationImpl
    {

        public int Create()
        {

            // Give sockets to any playable media types
            ContentDefinitionManager.AlterTypeDefinition("Video", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Audio", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Playlist", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Image", cfg => cfg
                .WithPart("SocketsPart"));

            // Build PlaylistItemPartRecord table
            SchemaBuilder.CreateTable("PlaylistItemPartRecord", table => table
                .ContentPartRecord()
                );

            // Menu connectors
            ContentDefinitionManager.AlterTypeDefinition("PlaylistItem", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("PlaylistItemPart")
                .WithPart("SequencePart")
                // TODO: Perhaps we also need a code-based way to limit types allowed, so we can actually check items to see if they're playable by a specific media player
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Playlist")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Image,Audio,Video")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "PlaylistItemContainer"))
                .WithSetting("Stereotype", "Content"));
            ContentDefinitionManager.AlterTypeDefinition("PlaylistItemContainer", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Image,Audio,Video")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Playlist")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "PlaylistItem"))
                .WithSetting("Stereotype", "Content"));

            return 1;
        }
    }
}